package org.apache.dolphinscheduler.api.controller;

import static org.apache.dolphinscheduler.api.enums.Status.QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR;

import org.apache.dolphinscheduler.api.aspect.AccessLogAnnotation;
import org.apache.dolphinscheduler.api.aspect.PermissionCheck;
import org.apache.dolphinscheduler.api.exceptions.ApiException;
import org.apache.dolphinscheduler.api.service.OverviewService;
import org.apache.dolphinscheduler.api.utils.Result;
import org.apache.dolphinscheduler.common.constants.Constants;
import org.apache.dolphinscheduler.dao.entity.User;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Parameter;

@Api(tags = "OVERVIEW")
@RestController
@RequestMapping("projects/overview")
public class OverviewController extends BaseController {
    private static final Logger logger = LoggerFactory.getLogger(OverviewController.class);

    @Autowired
    OverviewService overviewService;

    @GetMapping("/process-definition-num")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "home:list")
    public Result queryProcessDefinitionNums(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
        Integer num = overviewService.getProcessDefinitionNum(loginUser);
        return Result.success(num);
    }

    @GetMapping("/process-file-num")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "home:list")
    public Result queryFileNums(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
        Integer num = overviewService.getFileNum(loginUser);
        return Result.success(num);
    }

    @GetMapping("/process-udf-num")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "home:list")
    public Result queryUDFNums(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
        Integer num = overviewService.getUDFNum(loginUser);
        return Result.success(num);
    }

    @GetMapping("/process-datasource-num")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "home:list")
    public Result queryDatasourceNums(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
        Integer num = overviewService.getDatasourceNum(loginUser);
        return Result.success(num);
    }

    @GetMapping("/latest10")
    @ResponseStatus(HttpStatus.OK)
    @ApiException(QUERY_PROCESS_INSTANCE_LIST_PAGING_ERROR)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "scheduling:center:create")
    public Result queryLatest10ProcessInstance(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                               @RequestParam("pageNo") Integer pageNo,
                                               @RequestParam("pageSize") Integer pageSize) {
        return overviewService.queryProcessInstanceLatest10(loginUser, pageNo, pageSize);
    }

    @GetMapping("/statistics")
    @ResponseStatus(HttpStatus.OK)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "home:list")
    public Result queryHomeStatistics(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser) {
        Map<String, Object> result = overviewService.queryHomeStatistics(loginUser);
        return returnDataList(result);
    }

    @GetMapping("/process/scene")
    @ResponseStatus(HttpStatus.OK)
    @AccessLogAnnotation(ignoreRequestArgs = "loginUser")
    @PermissionCheck(permission = "home:list")
    public Result queryProcessScene(@Parameter(hidden = true) @RequestAttribute(value = Constants.SESSION_USER) User loginUser,
                                    @RequestParam(value = "startDate", required = false) String startDate,
                                    @RequestParam(value = "endDate", required = false) String endDate) {
        Map<String, Object> result = overviewService.queryProcessScene(loginUser, startDate, endDate);
        return returnDataList(result);
    }
}
